package cn.sunlelin.educationserver.service.Impl;

import cn.sunlelin.educationserver.entity.Task;
import cn.sunlelin.educationserver.entity.Taskscore;
import cn.sunlelin.educationserver.entity.User;
import cn.sunlelin.educationserver.mapper.TaskMapper;
import cn.sunlelin.educationserver.mapper.TaskscoreMapper;
import cn.sunlelin.educationserver.service.TaskscoreService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class TaskscoreServiceImpl implements TaskscoreService {
    @Autowired
    private TaskscoreMapper taskscoreMapper;
    @Autowired
    private TaskMapper taskMapper;

    @Override
    public Boolean createTaskscore(Taskscore taskscore) {
        if(this.selectTaskscoreById(taskscore.getTaskscoreId())!=null)
            return false;
        taskscore.setCreateTime(new Date());
        taskscoreMapper.createTaskscore(taskscore);
        return true;
    }

    @Override
    public Boolean updateTaskscore(Taskscore taskscore) {
        if(this.selectTaskscoreById(taskscore.getTaskscoreId())==null)
            return false;
        Task task=taskMapper.selectTaskById(taskscore.getTaskId());
        if(task.getSubmitnum()<taskscore.getSubmitNum())
            return false;
        taskscore.setUpdateTime(new Date());
        taskscoreMapper.updateTaskscore(taskscore);
        return true;
    }

    @Override
    public Boolean TaskscoreScoring(Taskscore taskscore, Integer userId){
        if(this.selectTaskscoreById(taskscore.getTaskscoreId())==null)
            return false;
        taskscoreMapper.TaskscoreScoring(taskscore);
        return true;
    }
    @Override
    public Taskscore selectTaskscoreById(Integer taskscoreId) {
        return taskscoreMapper.selectTaskscoreById(taskscoreId);
    }

    @Override
    public List<Taskscore> selectTaskscoreByUserId(Integer userId) {
        return taskscoreMapper.selectTaskscoreByUserId(userId);
    }

    @Override
    public List<Map<String,Object>> selectTaskscoreByTaskId(Integer taskId) {
        return taskscoreMapper.selectTaskscoreByTaskId(taskId);
    }

    @Override
    public Page<Map<String, Object>> getPageScoreByTaskId(Integer currentPage, Integer pageSize, Integer taskId) {
        PageHelper.startPage(currentPage,pageSize);
        List<Map<String,Object>> courseList=taskscoreMapper.selectTaskscoreByTaskId(taskId);
        return (Page<Map<String, Object>>) courseList;
    }

    @Override
    public Map<String,Object> selectTaskscoreByUserAndTask(Integer userId,Integer taskId) {
        Taskscore taskscore=new Taskscore();
        taskscore.setUserId(userId);
        taskscore.setTaskId(taskId);
        return taskscoreMapper.selectTaskscoreByUserAndTask(taskscore);
    }

    @Override
    public Boolean deleteTaskscore(Integer taskscoreId) {
        if(this.selectTaskscoreById(taskscoreId)==null)
            return false;
        Taskscore taskscore=new Taskscore();
        taskscore.setTaskscoreId(taskscoreId);
        taskscore.setUpdateTime(new Date());
        taskscoreMapper.deleteTaskscore(taskscore);
        return true;
    }
}
